package com.yanqu.road.server.cmd.activity.newyear2023.redpacket;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FireWorkProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.newyear2023.FireWorkActivity;
import com.yanqu.road.server.manager.activity.newyear2023.FireWorkMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_FIREWORK_ACTIVITY_ROB, desc = "抢红包")
public class CrossFireWorkRobCmd extends CrossCommand {

    private int activityId;
    private long userId;
    private int no;

    private UserBaseInfo userBaseInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        FireWorkProto.CrossFireWorkRobRedPacketTableReqMsg reqMsg = FireWorkProto.CrossFireWorkRobRedPacketTableReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        no = reqMsg.getNo();
        userId = packet.getPlayerId();
        if(reqMsg.hasPlayerBaseData()){
            userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
            CrossUserMgr.updateUserBaseInfo(userId,userBaseInfo);
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        FireWorkActivity activity = FireWorkMgr.getFireWorkActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        ret = activity.robTable(serverId,no,userId);
    }

    @Override
    public void sendData() {
        if(ret != 0){
            FireWorkProto.FireWorkOpenRedPacketTableRespMsg.Builder respMsg = FireWorkProto.FireWorkOpenRedPacketTableRespMsg.newBuilder();
            respMsg.setRet(ret);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_FIREWORK_RP_JOIN, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

}
